/*     */ package com.zimbra.cs.datasource.imap;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.account.DataSource;
/*     */ import com.zimbra.cs.mailbox.Folder;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxLock;
/*     */ import com.zimbra.cs.mailbox.util.TypedIdList;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Collection;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ class FolderChanges
/*     */ {
/*     */   private final DataSource ds;
/*     */   private final Mailbox mbox;
/*     */   private List<FolderChange> changes;
/*     */   private int lastChangeId;
/*     */   
/*     */   public static FolderChanges getChanges(DataSource ds, int lastSync)
/*     */     throws ServiceException
/*     */   {
/*  38 */     return new FolderChanges(ds, ds.getMailbox()).findChanges(lastSync);
/*     */   }
/*     */   
/*     */   private FolderChanges(DataSource ds, Mailbox mbox) {
/*  42 */     this.ds = ds;
/*  43 */     this.mbox = mbox;
/*     */   }
/*     */   
/*     */ 
/*     */   private FolderChanges findChanges(int lastSync)
/*     */     throws ServiceException
/*     */   {
/*  50 */     this.mbox.lock.lock(false);
/*     */     List<Integer> tombstones;
/*  52 */     List<Folder> modifiedFolders; try { this.lastChangeId = this.mbox.getLastChangeID();
/*  53 */       if (this.lastChangeId <= lastSync) {
/*  54 */         return this;
/*     */       }
/*  56 */       tombstones = this.mbox.getTombstones(lastSync).getIds(MailItem.Type.FOLDER);
/*  57 */       modifiedFolders = this.mbox.getModifiedFolders(lastSync);
/*     */     } finally {
/*  59 */       this.mbox.lock.release();
/*     */     }
/*  61 */     if (((tombstones == null) || (tombstones.isEmpty())) && (modifiedFolders.isEmpty())) {
/*  62 */       return this;
/*     */     }
/*  64 */     this.changes = new ArrayList();
/*     */     
/*     */     Iterator i$;
/*  67 */     if (tombstones != null) {
/*  68 */       for (i$ = tombstones.iterator(); i$.hasNext();) { int id = ((Integer)i$.next()).intValue();
/*  69 */         ImapFolder tracker = getTracker(id);
/*  70 */         if (tracker != null) {
/*  71 */           this.changes.add(FolderChange.deleted(id, tracker));
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*     */ 
/*  77 */     for (Folder folder : modifiedFolders) {
/*  78 */       FolderChange change = getChange(folder);
/*  79 */       if (change != null) {
/*  80 */         this.changes.add(change);
/*     */       }
/*     */     }
/*     */     
/*  84 */     return this;
/*     */   }
/*     */   
/*     */   private FolderChange getChange(Folder folder) throws ServiceException {
/*  88 */     ImapFolder tracker = getTracker(folder.getId());
/*  89 */     if (tracker != null) {
/*  90 */       if (!folder.getPath().equals(tracker.getLocalPath())) {
/*  91 */         return FolderChange.moved(folder, tracker);
/*     */       }
/*     */     } else {
/*  94 */       return FolderChange.added(folder);
/*     */     }
/*  96 */     return null;
/*     */   }
/*     */   
/*     */   public boolean hasChanges() {
/* 100 */     return (this.changes != null) && (!this.changes.isEmpty());
/*     */   }
/*     */   
/*     */   public Collection<FolderChange> getChanges() {
/* 104 */     if (this.changes == null) {
/* 105 */       this.changes = new ArrayList();
/*     */     }
/* 107 */     return this.changes;
/*     */   }
/*     */   
/*     */   public int getLastChangeId() {
/* 111 */     return this.lastChangeId;
/*     */   }
/*     */   
/*     */   private ImapFolder getTracker(int folderId) throws ServiceException {
/*     */     try {
/* 116 */       return new ImapFolder(this.ds, folderId);
/*     */     } catch (MailServiceException.NoSuchItemException e) {}
/* 118 */     return null;
/*     */   }
/*     */   
/*     */ 
/*     */   public String toString()
/*     */   {
/* 124 */     int added = 0;int moved = 0;int deleted = 0;
/* 125 */     for (FolderChange change : getChanges()) {
/* 126 */       switch (change.getType()) {
/* 127 */       case ADDED:  added++; break;
/* 128 */       case MOVED:  moved++; break;
/* 129 */       case DELETED:  deleted++;
/*     */       }
/*     */     }
/* 132 */     return String.format("{changeId=%d,added=%d,moved=%d,deleted=%d", new Object[] { Integer.valueOf(this.lastChangeId), Integer.valueOf(added), Integer.valueOf(moved), Integer.valueOf(deleted) });
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/datasource/imap/FolderChanges.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */